//
//  HomeTableViewController.swift
//  LX_weibo
//
//  Created by LTT❤️ on 15/6/24.
//  Copyright © 2015年 LTT. All rights reserved.
//

import UIKit
import SVProgressHUD

class HomeTableViewController: BaseTableViewController {

    var statusesList:[Status]?{
        
        didSet {
            tableView.reloadData()
            
            //关闭刷新控件
            
            refreshControl?.endRefreshing()
        }
        
        
    }
    lazy var rowHeightCache = [Int:CGFloat]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
   visitorView?.setupViewInfo("visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜", isHome: true)
        
        if !userLogon {
            return
        }
        
        setupNavigationBar()
        
        loadData()
        
        //注册可重用cell 
        //tableView.registerClass(statusno
        //设置行高  预估行高
        // tableView.estimatedRowHeight = 200
        // tableView.rowHeight = UITableViewAutomaticDimension
        //删除分割线
        
        
        //刷新
        refreshControl = HomeRefresh()
  
        
        tableView.registerClass(StatusForwardCell.self, forCellReuseIdentifier: LTStatusForwardCellIdentifier)
        tableView.registerClass(StatusNormalCell.self, forCellReuseIdentifier: LTStatusNormalCellIdentifier)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        rowHeightCache.removeAll()
    }
    
  
    
    ///加载微博数据
    func loadData(){
        let since_id = statusesList?.first?.id ?? 0
        Status.loadStatus (since_id){ (statuses,error) -> () in
        if error != nil{
            SVProgressHUD.showInfoWithStatus("网络不给力")
            return
        }
        if statuses == nil{
            print("没有数据")
            return
        }
       //判断是否下拉刷新
            if since_id > 0 {
            self.statusesList = statuses! + self.statusesList!
            }else{
            self.statusesList = statuses
            }
    
        }
    }
    
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        visitorView?.startAnimation()
    }
    
    
    let popoverAnimator = PoppverAnimator()
    
    func titleButtonClick(){
        let sb = UIStoryboard(name: "FriendGroup", bundle: nil)
        let vc = sb.instantiateViewControllerWithIdentifier("FriendGroupSB")
        
        vc.transitioningDelegate = popoverAnimator
        //视图展示大小
        popoverAnimator.presentFrame = CGRectMake(100, 56, 200, 240)
        
        
        vc.modalPresentationStyle = UIModalPresentationStyle.Custom
        
        presentViewController(vc, animated: true, completion: nil)
        
        
    }
    
//表格的数据源方法
    //MARK:数据源方法
    //行数
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return statusesList?.count ?? 0
     }
    //cell
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let status = statusesList![indexPath.row]
        //1.定义cell 
        let ID = StatusCell.cellIdentifier(status)
        let cell = tableView.dequeueReusableCellWithIdentifier(ID, forIndexPath: indexPath) as! StatusCell
        //2.设置cell
        cell.status = status
        
        return  cell
    }
    
   
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let status = statusesList![indexPath.row]
        if  rowHeightCache[status.id] != nil{
            print("缓存行高")
            return rowHeightCache[status.id]!
        }
        //取可重用的 ID 
        let ID = StatusCell.cellIdentifier(status)
        //定义一个类默认是原创微博的cell
        var cls:AnyClass = StatusNormalCell.self
        
        if ID == LTStatusForwardCellIdentifier{
            cls = StatusForwardCell.self
        }
        let cell = cls.new() as! StatusCell
        
        
       // let cell = tableView.dequeueReusableCellWithIdentifier(StatusCell.cellIdentifier(status)) as! StatusCell
        let height = cell.rowHeight(status)
        rowHeightCache[status.id] = height
        return height
    }
    
    
   private func setupNavigationBar(){
    if  sharedUserAccount == nil {
        return
    }
    let leftBtn = UIButton()
    leftBtn.frame = CGRectMake(0, 0, 30, 30)
    leftBtn.setImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
    leftBtn.setImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
    
    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBtn)
    
    let rightBtn = UIButton()
    rightBtn.frame = CGRectMake(0, 0, 30, 30)
    rightBtn.setImage(UIImage(named: "navigationbar_pop"), forState: UIControlState.Normal)
    rightBtn.setImage(UIImage(named: "navigationbar_pop_highlighted"), forState: UIControlState.Highlighted)
    
    navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBtn)
    
    
    //设置标题
    let  name = sharedUserAccount!.name
    let  titleBtn = HomeTitleButton()
    titleBtn.frame = CGRectMake(0, 0, 120, 30)
    titleBtn.setTitle(name!+" ", forState: UIControlState.Normal)
    titleBtn.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
      titleBtn.setImage(UIImage(named: "navigationbar_arrow_up"), forState: UIControlState.Highlighted)
    
    titleBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    navigationItem.titleView = titleBtn
    titleBtn.titleLabel?.font = UIFont.systemFontOfSize(17)
    titleBtn.addTarget(self, action: "titleButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
    
        
    }
    //是否转场
    

}


